package org.example.service.impl;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.bean.Zh_user;
import org.example.mapper.ZHUserMapper;
import org.example.service.IZHUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ZHUserServiceImpl implements IZHUserService {

    @Autowired
    private ZHUserMapper zhUserMapper;

    @Override
    public JSONObject addUser(Zh_user userInfo) {
        JSONObject obj = new JSONObject();
        try{
            zhUserMapper.insertUser(userInfo);
            obj.put("code", "0");
        }catch(DataAccessException e) {
            System.out.println("--------------error----------------");
            System.out.println(e.getMessage());
            obj.put("code", "100");
        }
        return obj;
    }

    @Override
    public List<Map> getAllUser() {
        return zhUserMapper.getAllUser();
    }

    @Override
    public JSONObject deleteUser(String userId) {
        JSONObject obj = new JSONObject();
        try{
            zhUserMapper.deleteUser(userId);
            obj.put("code", "0");
            obj.put("msg", "删除成功");
        }catch(DataAccessException e) {
            System.out.println("--------------error----------------");
            System.out.println(e.getMessage());
            obj.put("code", "100");
            obj.put("msg", "删除失败");
        }
        return obj;
    }
}
